package com.dgshanger.sanhxiaofeisc.mvp.presenter;

import android.text.TextUtils;

import com.dgshanger.sanhxiaofeisc.entity.CheckPwdEntity;
import com.dgshanger.sanhxiaofeisc.mvp.contract.WithdrawContract;
import com.dgshanger.sanhxiaofeisc.mvp.model.WithdrawModel;
import com.dgshanger.sanhxiaofeisc.ui.person.purse.WithdrawSuccessActivity;
import com.xx.baseuilibrary.mvp.BaseRxMvpPresenter;
import com.xx.baseutilslibrary.network.rx.RxHttpObserver;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * WithdrawPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/5/8 16:32.
 */

public class WithdrawPresenter extends BaseRxMvpPresenter<WithdrawContract.Model, WithdrawContract.View> implements WithdrawContract.Presenter {

    @NotNull
    @Override
    protected WithdrawContract.Model createModel() {
        return new WithdrawModel();
    }

    @Override
    public void withdraw() {
        if (getView() == null) {
            return;
        }
        String account = getView().getAccount();
        String money = getView().getMoney();
        String payPwd = getView().getPayPwd();
        if (TextUtils.isEmpty(account)) {
            getView().showToast("请输入支付宝账户");
            return;
        }
        getModel().withdraw(account, money, payPwd, new RxHttpObserver<Object>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable Object entity) {
                if (getView() != null) {
                    getView().startActivityThenFinishSelf(WithdrawSuccessActivity.class);
                }
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() != null) {
                    getView().showToast(error);
                }
            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog("提现申请中...");
            }

            @Override
            public void onLoadingFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }

    @Override
    public void checkPayPwd() {
        getModel().checkPayPwd(new RxHttpObserver<CheckPwdEntity>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable CheckPwdEntity entity) {
                if (getView()==null){
                    return;
                }
                if ("0".equals(entity.getPwd_status())){
                    getView().onSettingPayPwd();
                }else {
                    getView().showPayPwdDialog();
                }
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView()==null){
                    return;
                }
                getView().showToast(error);
            }

            @Override
            public void onLoadingStart() {
                if (getView()==null){
                    return;
                }
                attachObserver(this);
                getView().showLoadingDialog();
            }

            @Override
            public void onLoadingFinish() {
                if (getView()==null){
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }
}
